/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-07-15 13:44:38
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-12-06 09:47:19
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import { MessageService } from '@abp/message/message.service';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { LazyLoadEvent } from 'primeng/api';
import { BomContentServiceProxy, MaterialsServiceProxy, MaterialsEditContentDto, CommonServiceProxy } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
@Component({
    selector: 'materialCode',
    templateUrl: './material-code.component.html',
    styleUrls: ['./material-code.component.less']
})
export class MaterialCodeComponent extends AppComponentBase {
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() editData: any;  //行数据
    @Input() title: string;  //标题
    saving = false;
    active = false;
    addFlag = false;
    addForm = {
        materialCode: '', materialname: '', materialProperties: 0,
        materialModel: undefined, sendTime: undefined, isUsed: true, remark: ''
    };
    modelOptions = [];
    sendTimeOptions = [];
    materialPropertiesOption = [
        {
            label: '请选择',
            value: ''
        }, {
            label: '整装',
            value: 0
        }, {
            label: '芯级',
            value: 4
        }, {
            label: '部件',
            value: 1
        }, {
            label: '组件',
            value: 2
        }, {
            label: '零件',
            value: 3
        },
    ];
    saveFlag = true;
    constructor(
        injector: Injector,
        private _bomContentServiceProxy: BomContentServiceProxy,
        private _messageService: MessageService,
        private _materialsServiceProxy: MaterialsServiceProxy,
        private _commonServiceProxy: CommonServiceProxy,
    ) {
        super(injector);
    }

    ngOnInit() {
        //获取型号下拉
        this.modelOptions = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._commonServiceProxy.getModelList()
            .pipe(finalize(() => { }))
            .subscribe(result => {
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item.label;
                    data.value = item.value;
                    this.modelOptions.push(data);
                }
            });
    }
    onChange(event) {
        this.sendTimeOptions = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._commonServiceProxy.getLotNoList(this.addForm.materialModel)
            .pipe(finalize(() => { }))
            .subscribe(result => {
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item.label;
                    data.value = item.value;
                    this.sendTimeOptions.push(data);
                }
            });
    }
    getAttribute(row) {
        switch (row) {
            case 0:
                return '整装';
            case 4:
                return '芯级';
            case 1:
                return '部件';
            case 2:
                return '组件';
            case 3:
                return '零件';
            default:
                return null
        }
    }
    //打开弹窗
    onShown(): void {
    }
    //查看新增任务
    show(): void {
        if (this.title == '修改任务') {
        } else {
        }
        this.active = true;
        this.modal.show();
    }
    //关闭事件
    close(): void {
        if (this.addFlag) {
            this._messageService.error('新增弹窗未保存！');
            return;
        }
        //清空form表单
        this.active = false;
        this.modal.hide();
    }
    choose(row) {
        this.modalSave.emit(row);
        this.close();
    }
    getTableList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this._bomContentServiceProxy.getSelectParentMaterials(
            undefined,
            undefined,
            undefined,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }
    add() {
        this.addFlag = true;
    }
    addSave() {
        let input = new MaterialsEditContentDto();
        input.materialNo = this.addForm.materialCode;
        input.materialName = this.addForm.materialname;
        input.materialProperty = this.addForm.materialProperties;
        input.modelId = this.addForm.materialModel;
        input.sendTimeId = this.addForm.sendTime;
        if (this.addForm.isUsed) {
            input.isUsed = 1;
        } else {
            input.isUsed = 0;
        }
        if (this.saveFlag) {
            this.saveFlag = false;
            this._materialsServiceProxy.createOrUpdateMaterialsContent(input)
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe((result) => {
                    this.saveFlag = true;
                    if (result.code == 0) {
                        this._messageService.success('保存成功');
                        this.addForm.isUsed = true;
                        this.addForm.materialCode = undefined;
                        this.addForm.materialModel = undefined;
                        this.addForm.materialProperties = 0;
                        this.addForm.materialname = undefined;
                        this.addForm.remark = undefined;
                        this.addForm.sendTime = undefined;
                        this.addFlag = false;
                        this.getTableList();
                    } else {
                        this._messageService.error(result.message)
                    }
                });
        }

    }
    save() {

    }

}
